package com.tfswue.lh.service;

import com.tfswue.lh.event.UserRegisteredEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

@Service
public class EventPublisherService implements ApplicationEventPublisherAware {
    
    private static final Logger logger = LoggerFactory.getLogger(EventPublisherService.class);
    private ApplicationEventPublisher publisher;

    @Override
    public void setApplicationEventPublisher(
            ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }

    /**
     * 发布用户注册事件
     * @param username 用户名
     * @param email 邮箱
     */
    public void publishUserRegistered(String username, String email) {
        try {
            UserRegisteredEvent event = new UserRegisteredEvent(this, username, email);
            logger.info("发布用户注册事件: {}", event);
            publisher.publishEvent(event);
        } catch (Exception e) {
            logger.error("发布用户注册事件失败: username={}, email={}", username, email, e);
            throw new RuntimeException("事件发布失败", e);
        }
    }
}